package com.wb.mas.ui.order;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.SPUtils;
import com.wb.mas.R;
import com.wb.mas.app.SPKeys;
import com.wb.mas.entity.OrderListEntity;
import com.wb.mas.ui.defer.DeferActivity;
import com.wb.mas.ui.refund.RefundActivity;

import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;

/**
 * Copyright (C) 2018
 * OrderItemViewModel
 * <p>
 * Description
 *
 * @author Administrator
 * @version 1.0
 * <p>
 * Ver 1.0, 2019/4/30 0030, Administrator, Create file
 */
public class OrderItemViewModel extends ItemViewModel<OrderViewModel> {

    public ObservableField<OrderListEntity> entity = new ObservableField<>();

    /**
     * 订单状态显示
     */
    public ObservableField<String> orderStatusShow = new ObservableField<>();

    /**
     * 借款日期显示
     */
    public ObservableField<Boolean> isBorrowDateShow = new ObservableField<>();

    /**
     * 逾期天数显示
     */
    public ObservableField<Boolean> isOverdueShow = new ObservableField<>();

    /**
     * 逾期天数显示
     */
    public ObservableField<Boolean> isDeferShow = new ObservableField<>();

    /**
     * 底部提示显示
     */
    public ObservableField<String> bottomTipShow = new ObservableField<>();

    /**
     * 状态颜色
     */
    public ObservableField<Integer> resId = new ObservableField<>();


    public OrderItemViewModel(@NonNull OrderViewModel viewModel, OrderListEntity orderEntity) {
        super(viewModel);
        entity.set(orderEntity);
        orderStatusShow.set(entity.get().getStatusShow(viewModel.getApplication()));
        if (entity.get().orderStatus == OrderListEntity.OrderStatus.STATUS_WAITREFUND || entity.get().orderStatus == OrderListEntity.OrderStatus.STATUS_DEFER) {
            isBorrowDateShow.set(true);
            isOverdueShow.set(false);
            bottomTipShow.set(viewModel.getString(R.string.order_borrow_refund_tip));
            isDeferShow.set(SPUtils.getInstance().getBoolean(SPKeys.SP_ISDEFER, false));
        } else if (entity.get().orderStatus == OrderListEntity.OrderStatus.STATUS_OVERDUE) {
            isBorrowDateShow.set(true);
            isOverdueShow.set(true);
            bottomTipShow.set(viewModel.getString(R.string.order_borrow_overdue_tip));
            isDeferShow.set(SPUtils.getInstance().getBoolean(SPKeys.SP_ISDEFER, false));
        } else {
            isBorrowDateShow.set(false);
            isOverdueShow.set(false);
            bottomTipShow.set("");
        }

        if (entity.get().orderStatus == OrderListEntity.OrderStatus.STATUS_AUDIT) {
            resId.set(Color.parseColor("#FFF4C756"));
        } else if (entity.get().orderStatus == OrderListEntity.OrderStatus.STATUS_WAITLOAN) {
            resId.set(Color.parseColor("#4DBCF2"));
        } else if (entity.get().orderStatus == OrderListEntity.OrderStatus.STATUS_WAITREFUND) {
            resId.set(Color.parseColor("#FF26D6AB"));
        } else if (entity.get().orderStatus == OrderListEntity.OrderStatus.STATUS_OVERDUE) {
            resId.set(Color.parseColor("#FFFF5959"));
        } else if (entity.get().orderStatus == OrderListEntity.OrderStatus.STATUS_CLEARED) {
            resId.set(Color.parseColor("#FFCBCBCB"));
        } else if (entity.get().orderStatus == OrderListEntity.OrderStatus.STATUS_REFUSEED) {
            resId.set(Color.parseColor("#FFFFB06D"));
        } else if (entity.get().orderStatus == OrderListEntity.OrderStatus.STATUS_DEFER) {
            resId.set(Color.parseColor("#3CA286"));
        }
    }

    /**
     * 立即还款点击事件
     */
    public BindingCommand refundOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putString("orderId", entity.get().getOrderId());
            bundle.putString("approvedAmt", entity.get().repayMoney);
            bundle.putInt("type", RefundActivity.TYPE_REFUND);
            viewModel.startActivity(RefundActivity.class, bundle);

        }
    });

    /**
     * 展期点击事件
     */
    public BindingCommand deferOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putString("orderId", entity.get().getOrderId());
            viewModel.startActivity(DeferActivity.class, bundle);

        }
    });

    public String getString(int resId) {
        return viewModel.getString(resId);
    }
}
